package cn.csc.module.shop.controller.admin.hotsearch;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.shop.controller.admin.hotsearch.vo.*;
import cn.csc.module.shop.convert.hotsearch.ShopHotSearchConvert;
import cn.csc.module.shop.dal.dataobject.hotsearch.ShopHotSearchDO;
import cn.csc.module.shop.service.hotsearch.ShopHotSearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 热搜")
@RestController
@RequestMapping("/shop/hot-search")
@Validated
public class ShopHotSearchController {

    @Resource
    private ShopHotSearchService hotSearchService;

    @PostMapping("/create")
    @ApiOperation("创建热搜")
    @PreAuthorize("@ss.hasPermission('shop:hot-search:create')")
    public CommonResult<BigInteger> createHotSearch(@Valid @RequestBody ShopHotSearchCreateReqVO createReqVO) {
        return success(hotSearchService.createHotSearch(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新热搜")
    @PreAuthorize("@ss.hasPermission('shop:hot-search:update')")
    public CommonResult<Boolean> updateHotSearch(@Valid @RequestBody ShopHotSearchUpdateReqVO updateReqVO) {
        hotSearchService.updateHotSearch(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除热搜")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:hot-search:delete')")
    public CommonResult<Boolean> deleteHotSearch(@RequestParam("id") BigInteger id) {
        hotSearchService.deleteHotSearch(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得热搜")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:hot-search:query')")
    public CommonResult<ShopHotSearchRespVO> getHotSearch(@RequestParam("id") BigInteger id) {
        ShopHotSearchDO hotSearch = hotSearchService.getHotSearch(id);
        return success(ShopHotSearchConvert.INSTANCE.convert(hotSearch));
    }

    @GetMapping("/list")
    @ApiOperation("获得热搜列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('shop:hot-search:query')")
    public CommonResult<List<ShopHotSearchRespVO>> getHotSearchList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<ShopHotSearchDO> list = hotSearchService.getHotSearchList(ids);
        return success(ShopHotSearchConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得热搜分页")
    @PreAuthorize("@ss.hasPermission('shop:hot-search:query')")
    public CommonResult<PageResult<ShopHotSearchRespVO>> getHotSearchPage(@Valid ShopHotSearchPageReqVO pageVO) {
        PageResult<ShopHotSearchDO> pageResult = hotSearchService.getHotSearchPage(pageVO);
        return success(ShopHotSearchConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出热搜 Excel")
    @PreAuthorize("@ss.hasPermission('shop:hot-search:export')")
    @OperateLog(type = EXPORT)
    public void exportHotSearchExcel(@Valid ShopHotSearchExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopHotSearchDO> list = hotSearchService.getHotSearchList(exportReqVO);
        // 导出 Excel
        List<ShopHotSearchExcelVO> datas = ShopHotSearchConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "热搜.xls", "数据", ShopHotSearchExcelVO.class, datas);
    }

}
